/*
 * @Date: 2023-01-22 03:27:45
 * @LastEditors: 恶名扬 2080156129@qq.com
 * @LastEditTime: 2023-01-24 05:13:33
 * @FilePath: \campus-repair-platform\server\common\sql_sentences\repair.js
 */
/**
 * select 查询报修申请
 * selectCount  总数查询
 * selectLimit  分页查询
 * idSelect     根据id获取报修申请信息
 * insert 添加报修申请
 * idUpdate     根据Id修改报修申请信息
 */

const util = require("../util")
const table = "`repair`"
const str =
  "s_number, s_name, hex(type_id) type_id, hex(a_id) a_id, type_name, problem, place, phone, p_name, ctime, state, atime, a_number, r_number, rtime, cause, hex(m_id) m_id,etime"
const state = {
  '-1': 'and state != 3 and state != 6 and state != 8',
  '-2': 'and (state < 3)',
  '-3': 'and (state = 4 or state = 5 or state = 7)',
  '-4': 'and (state = 7 or state = 5)',
}

exports.select = (data, type, field) => util.select(field || str, table, typeof (data) == "string" ? data : util.selectTransition(data, true) + (data.state < 0 ? state[data.state] : ''))

exports.selectCount = (data) =>
  util.selectCount(table, typeof (data) == "string" ? data : util.selectTransition(data) + (data.state < 0 ? state[data.state] : ''))

exports.selectGroup = (data, type, group = 'state') =>
  util.selectGroup(table, typeof (data) == "string" ? data : util.selectTransition(data), group)

exports.selectLimit = (data, page, pagesize) =>
  util.selectLimit(str, table, typeof (data) == "string" ? data : util.selectTransition(data) + (data.state < 0 ? state[data.state] : ''), page, pagesize, !data.state || data.state === 6 ? 'state,ctime desc' : 'ctime asc')

exports.idSelect = (id) => util.idSelect(str, table, id)

exports.insert = (data) =>
  util.insert(table, util.insertStr(data), util.insertTransition(data))

exports.idUpdate = (data, id, type) =>
  (util.idUpdate(table, util.updateTransition(data), id) + (type === 1 ? 'and (state = 3 or state = 1 )' : ''))